//
//  Laboratory.cpp
//  GPUProcessingTesting
//
//  Created by kadlubek47 on 12.07.2013.
//  Copyright (c) 2013 kadlubek47. All rights reserved.
//

#include <sstream>
#include "Laboratory.h"

#include "FMTypes.h"
#include "FilterModule.h"
#include "ChannelsInOut.h"
#include "ResultPath_conf.h"

#include "AudioStream.h"



#pragma mark Experiments

void Laboratory::firstExperiment() {
    
    Experiment firstExperiment( "FilterModule", resultsPath_ );
    
    //Making experiment
    {
        //Set parameters
        structIn_filterModule_testFunction str;
        //.
        
        //Execute test function
        firstExperiment.add_trialsSet(      filterModule_testFunction(str)       );
        //.
    }
    //.
    
    //make statistics
    firstExperiment.compute_statistics_for_trialsSet();
    //.
    
    //Setting properties
    firstExperiment.properties_.add                         (Properties::experiment_title, std::string("FilterModule first experiment"));
    
    firstExperiment.set_trialsSet_propierty_as_mainProperty (Properties::repetition         );
    firstExperiment.set_trialsSet_propierty_as_mainProperty (Properties::num_of_channels    );
    firstExperiment.set_trialsSet_propierty_as_mainProperty (Properties::impulse_response_N );
    firstExperiment.set_trialsSet_propierty_as_mainProperty (Properties::impulse_response_C );
    
    
    firstExperiment.set_trialsSet_propierty_to_print        (Properties::block_size);
    firstExperiment.set_trialsSet_propierty_to_print        (Properties::convolutionerType  );
    
    firstExperiment.set_trialsSet_propierty_to_print        (Properties::statistics_latency );
    firstExperiment.set_trialsSet_propierty_to_print        (Properties::statistics_linear  );
    firstExperiment.set_trialsSet_propierty_to_print        (Properties::statistics_average );
    firstExperiment.set_trialsSet_propierty_to_print        (Properties::statistics_ontime  );
    //.
    
    //Writing results
    firstExperiment.writeResultsTo_stdcout          (false);
    firstExperiment.writeResultsTo_file_makeRformat ();
    //.
}



#pragma mark test_functions

/*
properties set by test function: 

repetition
block_size
sample_rate
num_of_channels

length_of_impulse_response
domain                     
processing_method
*/
TrialsSet Laboratory::filterModule_testFunction ( structIn_filterModule_testFunction& _in ) {
    using namespace std;
    
    //Create AudioStream from audio file
    AudioStream audioStream(_in.inFileName, _in.blockSize);
    //.
    //Create FilterModule with given parameters
    FilterModule filterModule;
    filterModule.setParameters(_in.params);
    //.
    //Timers
    Timer timer;
    //.

    //first
    audioStream.nextBlock();
    filterModule.process( audioStream.channels_blocks_ );
    filterModule.reset();
    //.
    
    //Processing
    timer.start(Timer::MAIN_TIMER);
    do {
        timer.start(Timer::TRIAL_TIMER);
        filterModule.process( audioStream.channels_blocks_ );
        timer.stop (Timer::TRIAL_TIMER);
    } while( audioStream.nextBlock() );

    timer.stop(Timer::MAIN_TIMER);
    //Check - write result to file
    audioStream.close(resultsPath_/*+ std::string(RAW_DATA_FOLDER)*/ +_in.outFileName);
    //.
    //.
    
    //Prepare result TrialsSet<double>
    TrialsSet trialsSet;
    trialsSet.add_trials    ( timer.get_trialsTimings() );
    trialsSet.add_mainTime  ( timer.get_mainTiming()    );
    
    using namespace std;
    trialsSet.properties_.add( Properties::repetition,                  (unsigned int)  trialsSet.get_trialsNumber()                                        );
    trialsSet.properties_.add( Properties::block_size,                  (unsigned int)  _in.blockSize                                                       );
    trialsSet.properties_.add( Properties::sample_rate,                 (unsigned int)  audioStream.get_sampleRate()                                        );
    trialsSet.properties_.add( Properties::num_of_channels,             (unsigned int)  audioStream.get_numOfChannels()                                     );
    
    trialsSet.properties_.add( Properties::impulse_response_N,          (unsigned int)  filterModule.get_IR_N()                                             );
    trialsSet.properties_.add( Properties::impulse_response_C,          (unsigned int)  filterModule.get_IR_C()                                             );
    trialsSet.properties_.add( Properties::convolutionerType,           (std::string)   FMParamsInfo::getString(_in.params.operator[]<FMParamsInfo::convolutionerIDType>(FMParamsInfo::convolutionerID))  );
    //.
    
    return trialsSet;
}



